#include"thread.h"
#include"sock_init.h"
/* 可用以下数据测试
 * #define PORT 6000
 * #define IPSTR "127.0.0.1"
 * #define LISMAX 5
*/
int create_socket(struct info_data *p);
int accept_client(int sockfd);
int main()
{
    struct info_data dt;
    read_info_cnf(&dt);
    
    /* 启动tcp服务器端 */
    int sockfd = create_socket(&dt);
    if(-1 == sockfd)
    {
        printf("create sockfd err\n");
        exit(0);
    }
    while(1)
    {
        int c = accept_client(sockfd);
        if(-1 == c)
        {
            continue;
        }
        start_thread(c);
    }
    return 0;
}
/* */
int accept_client(int sockfd)
{
    if(-1 == sockfd)
    {
        return -1;
    }
    struct sockaddr_in caddr;
    int len = sizeof(caddr);

    int c = accept(sockfd, (struct sockaddr*)&caddr, &len);
    return c;
}
int create_socket(struct info_data *p)
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)return -1;
    struct sockaddr_in saddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(p->port);
    saddr.sin_addr.s_addr = inet_addr(p->ipstr);
    int res = bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
    if(-1 == res)return -1;
    if(listen(sockfd, p->lismax)==-1)return -1;
    return sockfd;
}
